ModEditor.GUI.EditBox = {}
ModEditor.GUI.EditBox.Internal = {}

function ModEditor.GUI.EditBox.Internal.InsertString(dest,src,pos)
	if pos == 0 then
		return src..dest
	elseif pos == #dest then
		return dest..src
	else
		return string.sub(dest,1,pos)..src..string.sub(dest,pos+1)
	end
end
function ModEditor.GUI.EditBox.Internal.IsNormalSymbol(code)
	return ((code >= string.byte("a")) and (code <= string.byte("z"))) or ((code >= string.byte("0")) and (code <= string.byte("9"))) or (code == string.byte(".")) or (code == string.byte("_"))
end
function ModEditor.GUI.EditBox:DrawEverything(x,y)
	set_color(0,0,0,1)
	if (self.Internal.Timer > 25) and (self.Activated) then
		draw_text(ModEditor.GUI.EditBox.Internal.InsertString(self.Text,"|",self.CursorPosition),x,y)
	else
		draw_text(ModEditor.GUI.EditBox.Internal.InsertString(self.Text," ",self.CursorPosition),x,y)
	end
	self.Internal.Timer = self.Internal.Timer + 1
	if self.Internal.Timer > 50 then
		self.Internal.Timer = 0
	end
end
function ModEditor.GUI.EditBox:OnKeyDown(code)
	if ModEditor.GUI.EditBox.Internal.IsNormalSymbol(code) then
		if (self.MaxLength == -1) or (string.len(self.Text) < self.MaxLength) then
			if ModEditor.Input.RightShift or ModEditor.Input.LeftShift then
				self.Text = ModEditor.GUI.EditBox.Internal.InsertString(self.Text,string.upper(string.char(code)),self.CursorPosition)
			else
				self.Text = ModEditor.GUI.EditBox.Internal.InsertString(self.Text,string.char(code),self.CursorPosition)
			end
			self.CursorPosition = self.CursorPosition + 1
		end
	elseif code == 8 then
		if string.len(self.Text) == 1 then
			if self.CursorPosition == 1 then
				self.Text = ""
				self.CursorPosition = 0
			end
		elseif (self.Text ~= "") and (self.CursorPosition ~= 0) then
			self.Text = string.sub(self.Text,1,self.CursorPosition-1)..string.sub(self.Text,self.CursorPosition+1)
			self.CursorPosition = self.CursorPosition - 1
		end
	elseif code == 276 then
		if self.CursorPosition > 0 then
			self.CursorPosition = self.CursorPosition - 1
		end
	elseif code == 275 then
		if self.CursorPosition < string.len(self.Text) then
			self.CursorPosition = self.CursorPosition + 1
		end
	end
	return 1
end
function ModEditor.GUI.EditBox:OnKeyUp(code)
	return 1
end
function ModEditor.GUI.EditBox:Activate()
	ModEditor.Input.KeyHook = self
	self.Activated = true
end
function ModEditor.GUI.EditBox:Deactivate()
	ModEditor.Input.KeyHook = nil
	self.Activated = false
end
function ModEditor.GUI.EditBox:MouseDown(x,y)
end
function ModEditor.GUI.EditBox:New(parent)
	local obj = ModEditor.GUI.BasicControl:New(parent)
	obj.Internal = {}
	obj.Activate = self.Activate
	obj.Deactivate = self.Deactivate
	obj.MouseDown = self.MouseDown
	obj.OnKeyDown = self.OnKeyDown
	obj.OnKeyUp = self.OnKeyUp
	obj.DrawEverything = self.DrawEverything
	obj.Internal.InsertString = self.InsertString
	obj.Internal.IsNormalSymbol = self.Internal.IsNormalSymbol
	obj.Text = ""
	obj.Internal.Timer = 0
	obj.CursorPosition = 0
	obj.Activated = false
	obj.MaxLength = -1
	return obj
end
